
package com.skytech.tax.customsdatawarehouse.fta.origincredential.domain.dao;


import com.skytech.tax.customsdatawarehouse.fta.origincredential.domain.entity.FtaOriginCredentialFile;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class FtaOriginCredentialFileRepository {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    private String existsOriginCredentialFileSql = "SELECT count(1) FROM fta_origin_credential_file WHERE id = ?";

    public boolean exists(String id) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsOriginCredentialFileSql, new Object[]{id}, Integer.class);
        return count != null && count > 0;
    }

    private String deleteOriginCredentialFileSql = "DELETE FROM fta_origin_credential_file WHERE id = ?";

    public void delete(String id) {
        this.namedParameterJdbcTemplate.getJdbcOperations().update(this.deleteOriginCredentialFileSql, id);
    }

    private final String insertOriginCredentialFileSql = "INSERT INTO fta_origin_credential_file (id, file, storageTime) VALUES (:id, :file, :storageTime)";

    public void save(FtaOriginCredentialFile ftaOriginCredentialFile) {
        ftaOriginCredentialFile.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.insertOriginCredentialFileSql, new BeanPropertySqlParameterSource(ftaOriginCredentialFile));
    }


    private String selectOriginCredentialFileSql = "SELECT file, storageTime FROM fta_origin_credential_file WHERE id = ?";

    public FtaOriginCredentialFile findByDocId(String docId) {
        return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(selectOriginCredentialFileSql,
                new Object[]{docId}, new BeanPropertyRowMapper<>(FtaOriginCredentialFile.class));
    }

}
